package com.yc.fav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.fav.util.MBHelper;
import com.yc.fav.dao.TagFavMapper;
import com.yc.fav.dao.TagMapper;
import com.yc.fav.entity.Fav;
import com.yc.fav.dao.FavMapper;
import com.yc.fav.entity.Tag;
import com.yc.fav.entity.TagFav;
import com.yc.fav.service.BizException;
import com.yc.fav.service.IFavService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.fav.util.Utils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2024-10-28
 */
@Service
public class FavServiceImpl extends ServiceImpl<FavMapper, Fav> implements IFavService {

    @Override
    public void create(Fav fav) throws BizException {
        // 验证用户输入
        Utils.checkEmpty(fav.getName(),"请输入网址名称", -1);
        Utils.checkEmpty(fav.getAddr(),"请输入网址", -2);
        Utils.checkEmpty(fav.getIntro(),"请输入网址介绍", -3);
        Utils.check(fav.getName().length()<2, "网址名称至少是2个字符", -4);
        // TODO 其他字段自行验证

        // 验证数据库是否包含该链接
        Integer count = MBHelper.execute(session -> {
            FavMapper mapper = session.getMapper(FavMapper.class);
            LambdaQueryWrapper<Fav> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Fav::getAddr, fav.getAddr());  // addr = ?
            lqw.eq(Fav::getUid, fav.getUid());    // uid = ?
            return mapper.selectCount(lqw);
        });
        Utils.check(count>0,"该链接已经收藏了", -5);

        // 保存记录: 3个表
        // 拆分分类
        List<Tag> tags = new ArrayList<>();
        if (fav.getTags().isBlank() == false) {
            String[] items = fav.getTags().split("[,;，；]+");
            for (String item : items) {
                if(item.isBlank() == false){
                    Tag t = new Tag();
                    t.setName(item);
                    t.setUid(fav.getUid());
                    tags.add(t);
                }
            }
        }

        MBHelper.execute(session -> {
            FavMapper favMapper = session.getMapper(FavMapper.class);
            TagMapper tagMapper = session.getMapper(TagMapper.class);
            TagFavMapper tagFavMapper = session.getMapper(TagFavMapper.class);
            favMapper.insert(fav);
            for (Tag tag : tags) {
                LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
                lqw.eq(Tag::getName, tag.getName());
                lqw.eq(Tag::getUid, tag.getUid());
                Tag dbTag = tagMapper.selectOne(lqw);
                if(dbTag == null){
                    // 没有分类, 则新增
                    tagMapper.insert(tag);
                } else {
                    tag.setId(dbTag.getId());
                }
                TagFav tagFav = new TagFav();
                tagFav.setFid(fav.getId());
                tagFav.setTid(tag.getId());
                tagFavMapper.insert(tagFav);
            }
           return null;
        });
    }
}
